<template>
    <div>
        <h2>修改积分页面</h2>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">积分</td>
                    <td><input type="text" v-model="state.jifen"></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="增加" @click="jia(num)" />
                        <input type="button" value="减少" @click="jia(-num)" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="提交" @click="calc(state.jifen)" />
                    </td>
                </tr>

            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, toRefs } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';
const router = useRouter()
const route = useRoute()
let data = reactive({
    state: {
        jifen: ''
    },
    num: 10

})
let { state, num } = toRefs(data)
onMounted(() => {
    getlist()
})
const getlist = () => {
    axios({
        url: "/api/Member/GetMember",
        method: "get",
        params: {
            id: route.query.id
        }
    })
        .then(res => {
            console.log(res.data)
            state.value.jifen = res.data.data.integral
        })
        .catch(err => {
            console.log(err)
        })
}
const jia = (num: any) => {
    
    if (num < 0 && Number(state.value.jifen) <= 0) {
        return
    }
    state.value.jifen = Number(state.value.jifen) + num
}

const calc = (num: any) => {
    if (state.value.jifen == "") {
        alert("积分不能为空")
        return
    }
    if (isNaN(Number(state.value.jifen)) == true) {
        alert("积分必须是数字")
        return
    }
    if (Number(state.value.jifen) < 0) {
        alert("积分不能为负数")
        return
    }
    axios({
        url: '/api/Member/UpdateMemberJifen',
        method: 'put',
        params: { id: route.query.id, jifen: state.value.jifen }
    })
        .then(res => {
            console.log(res)
            if (res.data.data == 1) {
                alert("修改成功")
                router.push('/list')
            }
            else {
                alert("修改失败")
            }
        })
        .catch(err => {
            console.log(err)
        })
}
</script>

<style scoped></style>